<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <template #header>
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>江苏传智播客教育科技股份有限公司</span>
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDept()">
                        添加子部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <!-- 自己 -->
        <!-- <el-trees>
          <template #default="{ data }">
            <ul>
              <li>{{ data }}</li>
              <li>index</li>
              <li>index</li>
            </ul>
          </template>
        </el-trees> -->
        <!-- elemnt -->
        <el-tree
          :data="departData"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager || "--" }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept(data, 1)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="addDept(data, 2)">
                          编辑部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="delDept(data)">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <AddDept
      ref="editDept"
      :all-depts="allDepts"
      :show="show"
      :curr-dept="currDept"
      @update-list="getDepartData"
      @close-age="show = $event"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/deparments'
import { transformTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
// import ElTrees from '@/components/tree'
export default {
  components: {
    AddDept
  },
  data () {
    return {
      departData:
        [
        ],
      show: false,
      currDept: null,
      // 所有部门信息
      allDepts: [],
      // 公司信息
      company: { name: '', manager: 'CEO' },
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created () {
    this.getDepartData()
  },

  methods: {

    // 新增部门
    addDept (currDept, type) {
      console.log('新增部门', currDept)
      this.currDept = currDept
      if (type === 2) {
        this.$refs.editDept.getDepartDetail(currDept.id)
      }
      this.show = true
    },
    // 删除部门
    async delDept (currDate) {
      // console.log('数据：', currDate)

      try {
        // 1.提示
        await this.$confirm(`确定删除${currDate.name}吗？`, '提示')
        // 2.判断只能删除没有子集的部门
        if (currDate.children && currDate.children.length > 0) {
          return this.$message.error('先删除子集部门，不能删除父集')
        }

        await delDepartments(currDate.id)
        // 3.重新渲染列表
        this.getDepartData()
        // 4.提示删除成功
        this.$message.success('删除部门成功！')
      } catch (error) {
        console.log(error)
      }
    },
    async getDepartData () {
      const { companyName, depts } = await getDepartments()
      // console.table(depts)

      this.company.name = companyName
      this.allDepts = depts
      // 注意：调用一次，避免引用问题
      this.departData = transformTreeData(depts)
    },
    handleNodeClick (data) {
      console.log(data)
    }
  }
}

</script>

<style lang="scss" scoped>
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
